package com.dahua.messagesyncsh.pojo;

import com.dahua.messagesyncsh.rabbitmq.pojo.H485;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
@Slf4j
public class T485 implements Serializable {
    private String equipmentId;

    private Long dataTime;

    private String manufacturer;

    private List<Integer[]> data;

    T485(H485 h485){
        LocalDateTime dateTime;
        try{
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd,HH:mm:ss");
            dateTime = LocalDateTime.parse(h485.getDataTime(), formatter);
            this.dataTime = dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        }catch (Exception e){
            String sb = "【T485】：" +
                    "时间格式错误" +
                    h485.toString();
            log.info(sb);
        }
        this.equipmentId  = h485.getEquipmentId();
        this.manufacturer = h485.getManufacturer();
        this.data = h485.getData();
    }

    public static T485 build(H485 h485){

        LocalDateTime dateTime;
        try{
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd,HH:mm:ss");
            dateTime = LocalDateTime.parse(h485.getDataTime(), formatter);
        }catch (Exception e){
            log.error("【T485】: 时间格式错误{}", e.toString());
            return null;
        }

        return T485.builder()
                .equipmentId(h485.getEquipmentId())
                .dataTime(dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli())
                .manufacturer(h485.getManufacturer())
                .data(h485.getData())
                .build();
    }

    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder();

        for (Integer[] integers : data) {
            sb.append("[").append(integers[0]).append(",").append(integers[1]).append("],");
        }

        return "T485{" +
                " \"equipmentId\":" + "\"" + equipmentId + '\"' +
                ", \"dataTime\":" + dataTime +
                ", \"manufacturer\":'" + "\"" + manufacturer + '\"' +
                ", \"data\":" + sb +
                '}';
    }
}
